<?php
/***********************************************
	This class would use the PHP DOM API to
	scrape the header tags from the article.
************************************************/
class DomApi {
	private $allH1Tags = NULL;
	private $allH2Tags = NULL;
	private $allH3Tags = NULL;
	private $allH4Tags = NULL;
	
	private $h1Count = NULL;
	private $h2Count = NULL;
	private $h3Count = NULL;
	private $h4Count = NULL;
	
	private $hTagsCountArray = NULL;
	private $selectedHTag = NULL;
	private $articleKeywords = NULL;
	
	/************************************************************/
	
	function __construct($scrapedContent) {
		$this->articleKeywords = array();
		
		$domObject = new DOMDocument();
		
		/***********************************************
			This would load the scraped content into
			the DOM object.
			
			Any error would be suppressed for safety.
			
			Nevertheless, errors would definitely
			exist since the scraped content would
			have inconsistent HTML coding.
		************************************************/
		@$domObject->loadHTML($scrapedContent);
		
		$this->getAllH1Tags($domObject);
		$this->getAllH2Tags($domObject);
		$this->getAllH3Tags($domObject);
		$this->getAllH4Tags($domObject);
		
		$this->sortHTagsCountArray();
		$this->identifyArticleKeywords();
	}
	
	/************************************************************/
	
	function getAllH1Tags($domObject) {
		$this->h1Count = 0;
		$this->allH1Tags = $domObject->getElementsByTagName('h1');
		
		foreach ($this->allH1Tags as $h1) {
			$this->h1Count++;
		}
		
		if ($this->h1Count > 24 || $this->h1Count < 7) {
			$this->h1Count = 0;
		}
	}
	
	/************************************************************/
	
	function getAllH2Tags($domObject) {
		$this->h2Count = 0;
		$this->allH2Tags = $domObject->getElementsByTagName('h2');
		
		foreach ($this->allH2Tags as $h2) {
			$this->h2Count++;
		}
		
		if ($this->h2Count > 24 || $this->h2Count < 7) {
			$this->h2Count = 0;
		}
	}
	
	/************************************************************/
	
	function getAllH3Tags($domObject) {
		$this->h3Count = 0;
		$this->allH3Tags = $domObject->getElementsByTagName('h3');
		
		foreach ($this->allH3Tags as $h3) {
			$this->h3Count++;
		}
		
		if ($this->h3Count > 24 || $this->h3Count < 7) {
			$this->h3Count = 0;
		}
	}
	
	/************************************************************/
	
	function getAllH4Tags($domObject) {
		$this->h4Count = 0;
		$this->allH4Tags = $domObject->getElementsByTagName('h4');
		
		foreach ($this->allH4Tags as $h4) {
			$this->h4Count++;
		}
		
		if ($this->h4Count > 24 || $this->h4Count < 7) {
			$this->h4Count = 0;
		}
	}
	
	/************************************************************/
	
	function sortHTagsCountArray() {
		$this->hTagsCountArray = array("h1" => $this->h1Count,
										"h2" => $this->h2Count,
										"h3" => $this->h3Count,
										"h4" => $this->h4Count
										);
		
		arsort($this->hTagsCountArray);
		reset($this->hTagsCountArray);
		$this->selectedHTag = key($this->hTagsCountArray);
	}
	
	/************************************************************/
	
	function identifyArticleKeywords() {
		switch ($this->selectedHTag) {
			case "h1":
				if ($this->h1Count != 0) {
					foreach ($this->allH1Tags as $h1) {
						array_push($this->articleKeywords, $h1);
					}
				}
			break;
			/************************************************************/
			case "h2":
				if ($this->h2Count != 0) {
					foreach ($this->allH2Tags as $h2) {
						array_push($this->articleKeywords, $h2);
					}
				}
			break;
			/************************************************************/
			case "h3":
				if ($this->h3Count != 0) {
					foreach ($this->allH3Tags as $h3) {
						array_push($this->articleKeywords, $h3);
					}
				}
			break;
			/************************************************************/
			case "h4":
				if ($this->h4Count != 0) {
					foreach ($this->allH4Tags as $h4) {
						array_push($this->articleKeywords, $h4);
					}
				}
			break;
		}
	}
	
	/************************************************************/
	
	function getArticleKeywords() {
		return $this->articleKeywords;
	}
}
?>